Unity2D:拖动prefab时播放实例化的prefab动画 您所在的位置:网站首页 unity 控制动画播放进度 Unity2D:拖动prefab时播放实例化的prefab动画

Unity2D:拖动prefab时播放实例化的prefab动画

#Unity2D:拖动prefab时播放实例化的prefab动画| 来源: 网络整理| 查看: 265

在我的2D游戏中,我遇到了类似的平台问题。我建议的解决方案是创建一个GameObject,作为你想要动画的当前项目,以及一个LayerMask,作为你的光线投射可以命中的对象的过滤器。您可以将此LayerMask与Physics2D.Raycast API结合使用,后者有一个以LayerMask为参数的重载方法。

首先创建一个新的层,可以通过转到场景中对象的右上角并访问“层”框来完成。一旦你创建了一个新的层(我称之为我的“项目”),确保你的预制的层是正确分配的。

然后,在场景中创建一个空对象,并将此脚本附加到该对象。在该对象上,您将看到一个下拉菜单,询问您的光线投射应该命中哪些层。指定它的“项目”层;这确保你的光线投射只能击中该层中的对象,所以点击游戏中的任何其他东西都不会产生任何效果。

using UnityEngine; public class ItemAnimation : MonoBehaviour { private GameObject itemToAnimate; private Animator itemAnim; [SerializeField] private LayerMask itemMask; private void Update() { if (Input.GetMouseButtonDown(0)) { CheckItemAnimations(); } else if (Input.GetMouseButtonUp(0) && itemToAnimate != null) //reset the GameObject once the user is no longer holding down the mouse { itemAnim.SetBool("draging", false); itemToAnimate = null; } } private void CheckItemAnimations() { Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero, 1, itemMask); if (hit) //if the raycast hit an object in the "item" layer { itemToAnimate = hit.collider.gameObject; itemAnim = itemToAnimate.GetComponent(); itemAnim.SetBool("draging", true); Debug.Log(itemToAnimate.name); } else //the raycast didn't make contact with an item { return; } } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有